package com.chen.mapper;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chen.entity.Classes;
import com.chen.vo.ClassesVO;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

/**
 * 班级相关Mapper
 */
public interface ClassesMapper extends BaseMapper<Classes> {

    /**
     * 获取全部班级信息
     *
     * @return
     */
    default List<ClassesVO> findAllClasses() {
        List<Classes> classes = selectList(Wrappers.lambdaQuery(Classes.class));
        return BeanUtil.copyToList(classes, ClassesVO.class);
    }

    /**
     * 根据年级查询班级信息
     * @param gradeId
     * @return
     */
    default List<ClassesVO> findClassesByGradeId(Integer gradeId) {
        Wrapper<Classes> queryWrapper = Wrappers.lambdaQuery(Classes.class).eq(Classes::getGradeId, gradeId);
        List<Classes> classes = selectList(queryWrapper);
        return BeanUtil.copyToList(classes, ClassesVO.class);
    }

    /**
     * 根据条件查询班级信息
     * @param gradeId
     * @param className
     * @param classId
     * @return
     */
    List<ClassesVO> findClassesByGradeIdAndClassNameAndClassId(@Param("gradeId") Integer gradeId, @Param("className") String className, @Param("classId") Integer classId);

    /**
     * 通过id获取班级信息
     * @param classId
     * @return
     */
    ClassesVO findClassesByClassId(@Param("classId") Integer classId);

    /**
     * 根据教师id获取班级信息
     * @param teacherId
     * @return
     */
    ClassesVO findClassesByTeacherId(@Param("teacherId") Integer teacherId);


    /**
     * 根据年级查看数量
     * @param gradeId
     * @return
     */
    default Long findCountByGradeId(Integer gradeId) {
        Wrapper<Classes> queryWrapper = Wrappers.lambdaQuery(Classes.class).eq(Classes::getGradeId, gradeId);
        return selectCount(queryWrapper);
    }

    /**
     * 根据教师查看数量
     * @param teacherId
     * @return
     */
    default Long findCountByTeacherId(Integer teacherId) {
        Wrapper<Classes> queryWrapper = Wrappers.lambdaQuery(Classes.class).eq(Classes::getTeacherId, teacherId);
        return selectCount(queryWrapper);
    }

    @MapKey("className")
    Map<String, Object> getStudentCountForClass(Integer gradeId);
}
